<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Db;
class Count extends Backend
{
    /**
     * 查看
     */
    public function index()
    {
        $stime=input('stime');
        $etime=input('etime');
        $pid=input('pid');
        $this->view->assign("stime", $stime);
        $this->view->assign("etime", $etime);
        $this->view->assign("pid", $pid);
        $category = Db::name('category')->where('pid','0')->where('type','department')->select()->toArray();
        $this->view->assign("category", $category);
        return $this->view->fetch();
    }
    /*
     * 获取数据
     * 1,平台总人数 商户总数,订单总额度,任务完成数,待完成任务数
     *
     */
    public function getData()
    {
        $stime=input('stime');
        $etime=input('etime');
        $pid=input('pid');
        $where_auser=null;
        $where_avideo=null;
        $where_exam=null;
        if(!empty($pid)){
            $cate_ids=pid_allcate($pid);
            $cate_ids[]=$pid;
            $where_auser['cate_id']=array('in',$cate_ids);

            $avideos=Db::name('avideo')->field('cate_id,id')->select();
            $avideo_ids=array();
            $cateids=$cate_ids;
            foreach ($avideos as $k=>$v){
                $cates=explode(',',$avideos[$k]['cate_id']);
                if(!empty(array_intersect($cateids,$cates))){
                    $avideo_ids[]=$avideos[$k]['id'];
                }
            }

            $where_avideo['id']=array('in',$avideo_ids);
            $where_exam['avideo_id']=array('in',$avideo_ids);
        }
        if($stime&&$etime){
            $auser = Db::name('auser')->where($where_auser)->whereTime('ctime',[$stime,$etime])->count();
            $avideo = Db::name('avideo')->where($where_avideo)->whereTime('ctime',[$stime,$etime])->count();
            $exam = Db::name('exam')->where($where_exam)->whereTime('ctime',[$stime,$etime])->count();

        }else{
            //平台总人数
            $auser = Db::name('auser')->where($where_auser)->count();
            //商户总数
            $avideo = Db::name('avideo')->where($where_avideo)->count();
            //订单总额度
            $exam = Db::name('exam')->where($where_exam)->count();

        }
        $arr = array(
            ['数据', '平台所有用户量', '平台所有上传视频量', '平台所有考试量'],
            ['数据',$auser,$avideo,$exam]
        );
        $this->success('ok','',$arr);
    }
}